/**@author Guy Franco
*This function is a GUI function used by the Supervisor
*It presents the supervisor with  panel filled with relevant info
**/



package gui;

import java.util.ArrayList;

import javax.swing.JPanel;

import Entities.RequestEntity;
import common.ChatIF;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.Box;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.JScrollPane;
import javax.swing.JButton;

import java.awt.Desktop;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ViewRequestPanel extends JPanel implements ChatIF
{
	private JTextField viewingTextField;
	private JTextField firstNameField;
	private JTextField lastNameField;
	private JTextField emailField;
	private JTextField departmentField;
	private JTextField titleField;
	private JTextField phoneField;
	private JTextField systemField;
	private JTextField dateField;
	private JTextArea currentArea;
	private JTextArea changeDescriptionArea;
	private JTextArea explanationArea;
	private String requestID;
	private String uid;
	private JButton btnViewFile;
	private JButton btnClose;
	private AcademicFrame frame;
	private JPanel previousPanel;
	private JButton btnViewRemarks;
	private String remarks;
	private boolean supervisor;
	private JButton btnSave;
	private String userID;
		
	
	public ViewRequestPanel(String requestID, AcademicFrame frame, JPanel previousPanel, boolean supervisor, String userID)
	{
		this.requestID = requestID;
		this.frame = frame;
		this.previousPanel=previousPanel;
		this.supervisor=supervisor;
		this.userID = userID;
		initialize();
		frame.setSize(550, 731);
		
	}
		private void initialize()
		{
			setLayout(null);
				
			JLabel lblViewing = new JLabel("Viewing Request");
			lblViewing.setBounds(144, 33, 99, 14);
			add(lblViewing);
			
			viewingTextField = new JTextField();
			viewingTextField.setEditable(false);
			viewingTextField.setBounds(253, 30, 99, 20);
			add(viewingTextField);
			viewingTextField.setColumns(10);
			
			JLabel lblFirstName = new JLabel("First name");
			lblFirstName.setBounds(32, 98, 73, 14);
			add(lblFirstName);
			
			firstNameField = new JTextField();
			firstNameField.setEditable(false);
			firstNameField.setBounds(104, 95, 141, 20);
			add(firstNameField);
			firstNameField.setColumns(10);
			
			JLabel lblInitiatorDetails = new JLabel("Initiator Details:");
			lblInitiatorDetails.setBounds(32, 68, 99, 14);
			add(lblInitiatorDetails);
			
			JLabel lblLastName = new JLabel("Last name");
			lblLastName.setBounds(32, 131, 73, 14);
			add(lblLastName);
			
			lastNameField = new JTextField();
			lastNameField.setEditable(false);
			lastNameField.setColumns(10);
			lastNameField.setBounds(104, 128, 141, 20);
			add(lastNameField);
			
			emailField = new JTextField();
			emailField.setEditable(false);
			emailField.setColumns(10);
			emailField.setBounds(351, 95, 141, 20);
			add(emailField);
			
			JLabel lblEmail = new JLabel("E-mail");
			lblEmail.setBounds(279, 98, 73, 14);
			add(lblEmail);
			
			JLabel lblDepartment = new JLabel("Department");
			lblDepartment.setBounds(279, 131, 73, 14);
			add(lblDepartment);
			
			departmentField = new JTextField();
			departmentField.setEditable(false);
			departmentField.setColumns(10);
			departmentField.setBounds(351, 128, 141, 20);
			add(departmentField);
			
			titleField = new JTextField();
			titleField.setEditable(false);
			titleField.setColumns(10);
			titleField.setBounds(104, 162, 141, 20);
			add(titleField);
			
			JLabel lblTitle = new JLabel("Title");
			lblTitle.setBounds(32, 165, 73, 14);
			add(lblTitle);
			
			phoneField = new JTextField();
			phoneField.setEditable(false);
			phoneField.setColumns(10);
			phoneField.setBounds(351, 162, 141, 20);
			add(phoneField);
			
			JLabel lblPhone = new JLabel("Phone");
			lblPhone.setBounds(279, 165, 73, 14);
			add(lblPhone);
			
			JLabel lblRequestDetails = new JLabel("Request Details:");
			lblRequestDetails.setBounds(32, 200, 91, 14);
			add(lblRequestDetails);
			
			JLabel lblSystem = new JLabel("System");
			lblSystem.setBounds(32, 231, 73, 14);
			add(lblSystem);
			
			systemField = new JTextField();
			systemField.setEditable(false);
			systemField.setBounds(104, 228, 142, 20);
			add(systemField);
			systemField.setColumns(10);
			
			JLabel lblDate = new JLabel("Date");
			lblDate.setBounds(279, 231, 73, 14);
			add(lblDate);
			
			dateField = new JTextField();
			dateField.setEditable(false);
			dateField.setColumns(10);
			dateField.setBounds(351, 228, 142, 20);
			add(dateField);
			
			JLabel lblCurrent = new JLabel("Current status description");
			lblCurrent.setBounds(32, 256, 142, 14);
			add(lblCurrent);
			
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setBounds(31, 281, 461, 100);
			add(scrollPane);
			
			currentArea = new JTextArea();
			scrollPane.setViewportView(currentArea);
			currentArea.setBackground(UIManager.getColor("TextArea.disabledBackground"));
			currentArea.setEditable(false);
			

			JLabel lblChangeDescription = new JLabel("Change description");
			lblChangeDescription.setBounds(32, 392, 142, 14);
			add(lblChangeDescription);
			
			JScrollPane scrollPane_1 = new JScrollPane();
			scrollPane_1.setBounds(32, 410, 457, 105);
			add(scrollPane_1);
			
			changeDescriptionArea = new JTextArea();
			changeDescriptionArea.setEditable(false);
			
			scrollPane_1.setViewportView(changeDescriptionArea);
			changeDescriptionArea.setBackground(UIManager.getColor("TextArea.disabledBackground"));
			
			JLabel lblExplanation = new JLabel("Explanation");
			lblExplanation.setBounds(35, 526, 99, 14);
			add(lblExplanation);
			
			JScrollPane scrollPane_2 = new JScrollPane();
			scrollPane_2.setBounds(35, 545, 457, 101);
			add(scrollPane_2);
			
			explanationArea = new JTextArea();
			explanationArea.setEditable(false);
			
			scrollPane_2.setViewportView(explanationArea);
			explanationArea.setBackground(UIManager.getColor("TextArea.disabledBackground"));
			
			btnClose = new JButton("Close");
			btnClose.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					frame.setSize(640, 540);
					frame.setContentPane(previousPanel);	
				}
			});
			btnClose.setBounds(35, 657, 91, 23);
			add(btnClose);
			
			btnViewFile = new JButton("View File");
			btnViewFile.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					ArrayList<Object> message = new ArrayList<Object>();
					message.add("viewFile");
					message.add(requestID);
					AcademicFrame.myclient.sendMessageToServer(message);
				}
			});
			btnViewFile.setBounds(290, 657, 91, 23);
			add(btnViewFile);
			
			btnViewRemarks = new JButton("View Remarks");
			btnViewRemarks.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					RemarksFrame showRemarks = new RemarksFrame (remarks);
					showRemarks.setSize(700, 250);
					showRemarks.setVisible(true);
				}
			});
			btnViewRemarks.setEnabled(false);
			btnViewRemarks.setBounds(144, 657, 119, 23);
			add(btnViewRemarks);
			
			
			btnSave = new JButton("Save");
			
				
			btnSave.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					ArrayList<Object> updateFields =new ArrayList<Object>();
					updateFields.add("updateFields");
					updateFields.add(requestID);
					updateFields.add(currentArea.getText());
					updateFields.add(changeDescriptionArea.getText());
					updateFields.add(explanationArea.getText());
					updateFields.add(userID);
					AcademicFrame.myclient.sendMessageToServer(updateFields);
				}
			});
			
			btnSave.setBounds(403, 657, 89, 23);
			add(btnSave);
			
			JButton btnFetch = new JButton("Fetch");
			btnFetch.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					ArrayList<Object> getRequest = new ArrayList<Object>();
					getRequest.add("viewRequest");
					getRequest.add(requestID);
					AcademicFrame.myclient.sendMessageToServer(getRequest);
				}
			});
			btnFetch.setBounds(373, 29, 89, 23);
			add(btnFetch);
			if(supervisor)
				changeDescriptionArea.setEditable(true);
			if(supervisor)
				btnSave.setVisible(true);
			else
				btnSave.setVisible(false);
			if(supervisor)
				explanationArea.setEditable(true);
			if(supervisor)
				currentArea.setEditable(true);
		}
		
		
	@Override
	public void display(Object message)
	{
	
		
		ArrayList<Object> answer = new ArrayList<Object>();
		answer = (ArrayList<Object>)message;
		ArrayList<Object> object= new ArrayList<Object>();
		object= (ArrayList<Object>)message;

		
		
		if (	((String)(answer.get(0))).equalsIgnoreCase("requestData")	)
		{
			
				RequestEntity requestDetails = (RequestEntity)(answer.get(1));
				
				// Start filling the form
								
				this.viewingTextField.setText(requestID);
				this.dateField.setText(requestDetails.getD());
				this.currentArea.setText(requestDetails.getCSD());
				this.changeDescriptionArea.setText(requestDetails.getOCD());
				this.explanationArea.setText(requestDetails.getEXP());
				this.uid = requestDetails.getUid();
				this.systemField.setText(requestDetails.getSystem());
				this.remarks = requestDetails.getRE();
				
				if (!requestDetails.getFIN().equalsIgnoreCase("null"))
					this.btnViewFile.setEnabled(true);
				if (!(requestDetails.getRE().equalsIgnoreCase("empty")))
					this.btnViewRemarks.setEnabled(true);
				
				
				ArrayList<Object> getInitiatorInfo = new ArrayList<Object>();
				getInitiatorInfo.add("getInfo");
				getInitiatorInfo.add(uid);
				
				AcademicFrame.myclient.sendMessageToServer(getInitiatorInfo);
		}
		else if(((String)(object.get(0))).equalsIgnoreCase("viewFile"))
		{
			if(((String)(object.get(1))).equalsIgnoreCase("valid"))
			{
				byte[] barr = (byte[]) (object.get(2));
				try
				{
					if (barr != null) {
						Number fileNumber = Math.random() * 1000;
						String fileName = fileNumber.toString() + ".pdf";
						// write the inputStream to a FileOutputStream
						BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName));
						bos.write(barr);	
						bos.flush();
						bos.close();	
						Desktop.getDesktop().open(new File(fileName));
					}
					else
					{
						System.out.println("no file");
						(new Dialog ("There is no file for this request.")).setVisible(true);
					}
				}
				catch (IOException ex) {
					// TODO: handle exception
					System.out.println("error");
					(new Dialog ("There is no file for this request.")).setVisible(true);
				}
			}
			else
			{
				System.out.println("no file");
				(new Dialog ("There is no file for this request.")).setVisible(true);
			}
		}		
		else
			if (	((String)(answer.get(0))).equalsIgnoreCase("userInfo")	)
				{
					this.firstNameField.setText((String)answer.get(1));
					this.lastNameField.setText((String)answer.get(2));
					this.emailField.setText((String)answer.get(3));
					this.titleField.setText((String)answer.get(4));
					this.departmentField.setText((String)answer.get(5));
					this.phoneField.setText((String)answer.get(6));					
				}	
			else
				if (	((String)(answer.get(0))).equalsIgnoreCase("updateFields")	)
					{
					if (	((String)(answer.get(1))).equalsIgnoreCase("success")	)
					{
						(new Dialog("Changed and Saved")).setVisible(true);
						frame.setContentPane(previousPanel);
						frame.setSize(500, 401);

					}	
					else
						if (	((String)(answer.get(1))).equalsIgnoreCase("Fail")	)
						{
							(new Dialog("Fail")).setVisible(true);

						}	
					}
			}
	public String getrequestID()
	{
		return requestID;
	}
	
}
